package com.ruoyi.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.system.domain.vo.SysAreaVo;
import com.ruoyi.system.domain.bo.SysAreaBo;
import com.ruoyi.system.service.ISysAreaService;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 区域架构控制器
 * 前端访问路由地址为:/system/area
 *
 * @author xhw
 * @date 2023-07-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/area")
public class SysAreaController extends BaseController {

    private final ISysAreaService iSysAreaService;

    /**
     * 查询区域架构列表
     */
    @SaCheckPermission("system:area:list")
    @GetMapping("/list")
    public R<List<SysAreaVo>> list(SysAreaBo bo) {
        List<SysAreaVo> list = iSysAreaService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出区域架构列表
     */
    @SaCheckPermission("system:area:export")
    @Log(title = "区域架构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysAreaBo bo, HttpServletResponse response) {
        List<SysAreaVo> list = iSysAreaService.queryList(bo);
        ExcelUtil.exportExcel(list, "区域架构", SysAreaVo.class, response);
    }

    /**
     * 获取区域架构详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:area:query")
    @GetMapping("/{id}")
    public R<SysAreaVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iSysAreaService.queryById(id));
    }

    /**
     * 新增区域架构
     */
    @SaCheckPermission("system:area:add")
    @Log(title = "区域架构", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysAreaBo bo) {
        return toAjax(iSysAreaService.insertByBo(bo));
    }

    /**
     * 修改区域架构
     */
    @SaCheckPermission("system:area:edit")
    @Log(title = "区域架构", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysAreaBo bo) {
        return toAjax(iSysAreaService.updateByBo(bo));
    }

    /**
     * 删除区域架构
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:area:remove")
    @Log(title = "区域架构", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iSysAreaService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
